package com.imooc.service.impl.center;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.imooc.enums.OrderStatusEnum;
import com.imooc.mapper.CenterOrderCustomMapper;
import com.imooc.pojo.OrderStatus;
import com.imooc.pojo.vo.MyOrdersVO;
import com.imooc.pojo.vo.OrderStatusCountsVO;
import com.imooc.service.base.BaseService;
import com.imooc.service.center.CenterOrderService;
import com.imooc.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class CenterOrderServiceImpl extends BaseService implements CenterOrderService {

    @Autowired
    private CenterOrderCustomMapper centerOrderCustomMapper;

    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    @Override
    public PagedGridResult getMyOrders(String userId, Integer status, Integer page, Integer pageSize) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("userId", userId);
        if (null != status) {
            map.put("status", status);
        }
        List<MyOrdersVO> myOrders = centerOrderCustomMapper.getMyOrders(map);
        PageHelper.startPage(page, pageSize);
        return setterPagedGrid(myOrders, page);
    }

    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    @Override
    public OrderStatusCountsVO getMyOrderStatus(String userId) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("orderStatus", OrderStatusEnum.WAIT_DELIVER);
        Integer waitDeliver = this.centerOrderCustomMapper.getMyOrderStatus(map);
        map.put("orderStatus", OrderStatusEnum.WAIT_RECEIVE);
        Integer waitReceive = this.centerOrderCustomMapper.getMyOrderStatus(map);
        map.put("orderStatus", OrderStatusEnum.WAIT_PAY);
        Integer waitPay = this.centerOrderCustomMapper.getMyOrderStatus(map);
        map.put("orderStatus", OrderStatusEnum.SUCCESS);
        Integer success = this.centerOrderCustomMapper.getMyOrderStatus(map);
        OrderStatusCountsVO orderStatusCountsVO = new OrderStatusCountsVO();
        orderStatusCountsVO.setWaitDeliverCounts(waitDeliver);
        orderStatusCountsVO.setWaitPayCounts(success);
        orderStatusCountsVO.setWaitReceiveCounts(waitReceive);
        orderStatusCountsVO.setWaitCommentCounts(waitPay);
        return orderStatusCountsVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    @Override
    public PagedGridResult getMyTrend(String userId, Integer page, Integer pageSize) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("userId", userId);
        List<OrderStatus> myTrends = this.centerOrderCustomMapper.getMyTrend(map);
        PageHelper.startPage(page, pageSize);
        return setterPagedGrid(myTrends, page);
    }

}
